﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Net;
using System.Web;

using System.Xml.Linq;

namespace TwitterFlow
{
    public partial class GetUserStatus : Activity
    {
        public GetUserStatus()
        {
            InitializeComponent();
        }

        public static DependencyProperty UserIdProperty = System.Workflow.ComponentModel.DependencyProperty.Register("UserId", typeof(string), typeof(GetUserStatus));

        [Description("User Id to retrieve status")]
        [Category("Input")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string UserId
        {
            get
            {
                return ((string)(base.GetValue(GetUserStatus.UserIdProperty)));
            }
            set
            {
                base.SetValue(GetUserStatus.UserIdProperty, value);
            }
        }


        public static DependencyProperty StatiiProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Statii", typeof(IList<TwitterStatus>), typeof(GetUserStatus));

        [Description("blah")]
        [Category("output")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public IList<TwitterStatus> Statii
        {
            get
            {
                return ((IList<TwitterStatus>)(base.GetValue(GetUserStatus.StatiiProperty)));
            }
            set
            {
                base.SetValue(GetUserStatus.StatiiProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            // need to do a get on a url
            HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(string.Format("http://twitter.com/statuses/user_timeline/{0}.xml", UserId));
            HttpWebResponse wr = (HttpWebResponse)hwr.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(wr.GetResponseStream());
            string responseString = sr.ReadToEnd();
            List<TwitterStatus> lts = new List<TwitterStatus>();
           XDocument xd = XDocument.Parse(responseString);
            XElement baseElement = xd.Element("statuses");
            foreach (XElement xe in baseElement.Elements("status"))
            {
                TwitterStatus ts = new TwitterStatus();
                ts.ID = xe.Element("id").Value;
                ts.Status = xe.Element("text").Value;
                ts.CreatedAt = xe.Element("created_at").Value;
                ts.User = xe.Element("user").Element("screen_name").Value;
                lts.Add(ts);
            }
            Statii = lts;
            return ActivityExecutionStatus.Closed;
        }
    }
}
